package utils

import (
	"wisdomCoach/model"

	"github.com/carmel/base64Captcha"
	"golang.org/x/text/cases"
	"golang.org/x/text/language"
)

// 生成验证码图片
func CreateCodePhoto() (string, string) {
	//create base64 encoding captcha
	//创建base64图像验证码
	var config = base64Captcha.ConfigCharacter{
		Height: 60,
		Width:  160,
		//const CaptchaModeNumber:数字,CaptchaModeAlphabet:字母,CaptchaModeArithmetic:算术,CaptchaModeNumberAlphabet:数字字母混合.
		Mode:               base64Captcha.CaptchaModeNumber,
		ComplexOfNoiseText: base64Captcha.CaptchaComplexLower,
		ComplexOfNoiseDot:  base64Captcha.CaptchaComplexLower,
		IsShowHollowLine:   false,
		IsShowNoiseDot:     false,
		IsShowNoiseText:    false,
		IsShowSlimeLine:    false,
		IsShowSineLine:     false,
		CaptchaLen:         6,
	}
	//GenerateCaptcha 第一个参数为空字符串,包会自动在服务器一个随机种子给你产生随机uiid.
	captchaId, digitCap := base64Captcha.GenerateCaptcha("", config)
	base64Png := base64Captcha.CaptchaWriteToBase64Encoding(digitCap)

	//or you can do this
	//你也可以是用默认参数 生成图像验证码
	//base64Png := captcha.GenerateCaptchaPngBase64StringDefault(captchaId)
	return captchaId, base64Png

}

// 校验验证码
func VerifyCode(code string, uuid string) bool {
	return base64Captcha.VerifyCaptcha(uuid, code)
}

// 切片去重
func UniqueStringSlice(slice []string) []string {
	seen := make(map[string]bool)
	result := []string{}

	for _, value := range slice {
		if _, ok := seen[value]; !ok {
			seen[value] = true
			result = append(result, value)
		}
	}
	return result
}

// 菜单去重
func UniqueMenu(items []model.Menu) []model.Menu {
	idSet := make(map[int]bool)
	var result []model.Menu

	for _, item := range items {
		if _, exists := idSet[int(item.MenuId)]; !exists {
			result = append(result, item)
			idSet[int(item.MenuId)] = true
		}
	}
	return result
}

// 树形菜单
type Mate struct {
	Icon    string `json:"icon"`
	Link    string `json:"link"`
	NoCache bool   `json:"noCache"`
	Title   string `json:"title"`
}
type TreeMenuStruct struct {
	Id         int64               `json:"-"`
	ParentId   int64               `json:"-"`
	AlawysShow bool                `json:"alwaysShow"`
	Children   []*TreeMenuChildren `json:"children"`
	Component  string              `json:"component"`
	Hidden     bool                `json:"hidden"`
	Mate       Mate                `json:"meta"`
	Name       string              `json:"name"`
	Path       string              `json:"path"`
	Redirect   string              `json:"redirect"`
}
type TreeMenuChildren struct {
	Component string `json:"component"`
	Hidden    bool   `json:"hidden"`
	Mate      Mate   `json:"meta"`
	Name      string `json:"name"`
	Path      string `json:"path"`
}

func TreeMenu(items []model.Menu) []*TreeMenuStruct {
	nodeMap := make(map[int]*TreeMenuStruct)
	for _, node := range items {
		if node.MenuType == "F" {
			continue
		}
		TreeMenuStruct := &TreeMenuStruct{
			Id:         node.MenuId,
			ParentId:   node.ParentId,
			AlawysShow: true,
			Component:  node.Component,
			Hidden:     false,
			Mate:       Mate{Icon: node.Icon, Link: node.Path, NoCache: false, Title: node.MenuName},
			Name:       node.Path,
			Path:       "/" + node.Path,
			Redirect:   "",
		}
		if node.MenuType == "M" {
			TreeMenuStruct.Component = "Layout"
		}
		if node.IsFrame == 1 {
			TreeMenuStruct.Mate.Link = ""
		}

		titleCase := cases.Title(language.English)
		TreeMenuStruct.Name = titleCase.String(TreeMenuStruct.Name)
		nodeMap[int(node.MenuId)] = TreeMenuStruct
	}
	// 构建树形结构
	var rootNodes []*TreeMenuStruct
	for _, node := range items {
		if node.MenuType == "F" {
			continue
		}

		if node.ParentId == 0 {
			rootNodes = append(rootNodes, nodeMap[int(node.MenuId)])
		} else {
			//子节点
			parentNode := nodeMap[int(node.ParentId)]

			if parentNode != nil {
				treeMenuChildren := &TreeMenuChildren{
					Component: node.Component,
					Hidden:    false,
					Mate:      Mate{Icon: node.Icon, Link: node.Path, NoCache: false, Title: node.MenuName},
					Name:      node.Path,
					Path:      node.Path,
				}
				if node.IsFrame == 1 {
					treeMenuChildren.Mate.Link = ""
				}
				titleCase := cases.Title(language.English)
				treeMenuChildren.Name = titleCase.String(treeMenuChildren.Name)
				parentNode.Children = append(parentNode.Children, treeMenuChildren)
			}

		}
	}
	return rootNodes
}

// 树形部门
type TreeDeptStruct struct {
	Id       int64             `json:"id"`
	Pid      int64             `json:"-"`
	Label    string            `json:"label"`
	Children []*TreeDeptStruct `json:"children"`
}

func TreeDept(items []*model.Dept) []*TreeDeptStruct {

	var nodes []*TreeDeptStruct
	for _, item := range items {
		node := &TreeDeptStruct{
			Id:       item.DeptId,
			Pid:      item.ParentId,
			Label:    item.DeptName,
			Children: nil,
		}
		nodes = append(nodes, node)
	}

	treeLists := Tree(nodes, 0)
	return treeLists
}

func Tree(node []*TreeDeptStruct, pid int64) []*TreeDeptStruct {
	res := make([]*TreeDeptStruct, 0)
	for _, v := range node {
		if v.Pid == pid {
			v.Children = Tree(node, v.Id)
			res = append(res, v)
		}
	}
	return res
}
